﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generic
{
    class Program
    {
        //泛型节点
        public class Node<T>
        {
            public Node<T> Next { get; set; }
            public T Data { get; set; }
            public Node(T t)
            {
                Next = null;
                Data = t;
            }
        }
        //泛型链表类
        public class GenericList<T>
        {
            private Node<T> head;
            private Node<T> tail;

            public GenericList()
            {
                tail = head = null;
            }
            public Node<T> Head
            {
                get => head;
            }
            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if (tail == null)
                {
                    head = tail = n;
                }
                else
                {
                    tail.Next = n;
                    tail = n;
                }
            }

            public void Foreach(Action<T> action)
            {
                Node<T> t = this.head;
                while (t != null)
                {
                    action(t.Data);
                    t = t.Next;
                }
            }
        }
        static void Main(string[] args)
        {
            //整形List
            GenericList<int> intlist = new GenericList<int>();
            for(int x = 0; x < 10; x++)
            {
                intlist.Add(x);
            }
            //打印链表元素
            intlist.Foreach(data => Console.WriteLine(data));
            //求最大值
            int max = int.MinValue;
            intlist.Foreach(data => max = data > max ? data : max);
            Console.WriteLine("最大值:"+max);
            //求最小值
            int min = int.MaxValue;
            intlist.Foreach(data => min = data < min ? data : min);
            Console.WriteLine("最小值:" + min);
            //求和
            int sum = 0;
            intlist.Foreach(data => sum += data);
            Console.WriteLine("总和:" + sum);
        }
    }
}
